import QtQuick
import QtQuick.Window

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.2

ApplicationWindow {
    id: rootWindowtest
    title: qsTr("Qt基于Qml堆栈(StackView)窗口使用示例")
    width: fullScreen ? 640 : Screen.desktopAvailableWidth
    height: fullScreen ? 480 : Screen.desktopAvailableHeight
    visible: true
    flags: Qt.Window | Qt.FramelessWindowHint | Qt.WindowFullScreen

    property bool fullScreen: false
    
    property string examName
    property string posName
    property string posId
    property string batchName
    property int batchCount
     

    //窗口栈
    StackView {
        id: stackTest
        initialItem: pageTaskD //初始化时显示MainNavigator，此变量在下文有声明
        anchors.fill: parent
        
        Keys.enabled: true  // 不设置按键使能，获取不了按键事件
        focus: true         // 不设置焦点，获取不了键盘事件

        //动态创建页面
        function custPush(page, params){
            // console.log("say"+page);//控制台打印日志
            console.log('custPush ===> createComponent() ===> createObject()')
            let comp = Qt.createComponent(page)
            // comp.visible = true;
            var object = comp.createObject();
            object.stack = stack;
            if(params){
                for(var i in params){
                    console.log('custPush: ', i, params[i]);
                    object[i] = params[i];
                }
            }
            if(typeof object.initialAction == 'function'){
                object.initialAction();
            }
            // pageCounter.stack = stack;
            stack.push(object);//入栈
        }

        //弹出后销毁
        function custPop(){
            let object = stack.pop();//入栈
            if(typeof object.beforeDestroy == 'function'){
                object.beforeDestroy();
            }
            object.destroy();
            console.log('custPop ===> destroy()')
            // delete object;
        }
    }

    Component.onCompleted:{
        //http://t.zoukankan.com/DreamDog-p-9160017.html
        showFullScreen()
        // stack.custPush('MainNavigator.qml')
    }

    Popup {
        id:             popupCenter
        modal:          true            //模态， 为 true后弹出窗口会叠加一个独特的背景调光效果
        focus:          true            //焦点,  当弹出窗口实际接收到焦点时，activeFocus将为真
        padding:        0
        closePolicy:    Popup.CloseOnEscape | Popup.CloseOnPressOutside
        property var    raiseItem:          null
        background: Rectangle {
            color:      Qt.rgba(0,0,0,0)   //背景为无色
        }
        Loader {
            id: loaderCenter
            onLoaded: {
                popupCenter.x = (rootWindowtest.width - loaderCenter.width) * 0.5
                popupCenter.y = (rootWindowtest.height - loaderCenter.height) * 0.5
            }
        }
        onOpened: {
            loaderCenter.sourceComponent = popupCenter.raiseItem
        }
        onClosed: {
            loaderCenter.sourceComponent = null
            popupCenter.raiseItem = null
        }
    }

    function popupShowLoading() {
        popupCenter.raiseItem = popupLoadingComp
        popupCenter.open()
    }

    function popupHideLoading() {
        popupCenter.close()
    }

    function popupTips(text, delays, cb) {

        poptipText = text;
        popupCenter.raiseItem = popupTipsComp;
        popupCenter.open();

        if(!delays){
            delays = 3000;
        }
        timer.setTimeout(function(){ 
            // console.log("triggered");
            if(typeof cb === 'function'){
                cb()
            }
            popupCenter.close()
        }, delays);
    }
    
    property string poptipText:'...'

    Component {
        id: popupLoadingComp
        Rectangle {
            width:                  lab1.width + 30
            height:                 lab1.height + 30
            radius:                 20
            color:                  "#FF9D6F"
            border.width:           4
            border.color:           "black"
            Label {
                id:                 lab1
                anchors.centerIn:   parent
                font.bold:          true
                font.pointSize:     20
                text:               '正在加载...'
            }
        }
    }

    Timer {
        id: timer
        function setTimeout(cb, delayTime) {
            timer.interval = delayTime;
            timer.repeat = false;
            timer.triggered.connect(cb);
            timer.triggered.connect(function release () {
                timer.triggered.disconnect(cb); // This is important
                timer.triggered.disconnect(release); // This is important as well
                timer.stop();
            });
            timer.start();
        }
    }

    Component { //https://doc.qt.io/qt-5/qml-qtqml-component.html
        id: popupTipsComp
        Rectangle {
            width:                  lab2.width + 30
            height:                 lab2.height + 30
            radius:                 20
            color:                  "#FF9D6F"
            border.width:           4
            border.color:           "black"
            Label {
                id: lab2
                objectName: "lab2"
                anchors.centerIn:   parent
                font.bold:          true
                font.pointSize:     20
                text:               `${poptipText}`
                // Component.onCompleted:{
                //     // gNetwork.netstateSig.connect(mainView.afterNetchange)
                //     // mainView.afterNetchange.connect(gNetwork.netstateSig)
                //     lab2.text = poptipText;
                // }
            }
        }
    }

    PageTaskD {
        id: pageTaskD
        visible: false
    }

    // PageCounter {
    //     id: pageCounter
    //     visible: false
    // }

    // PageSetting {
    //     id: pageSetting
    //     visible: false
    // }
}